package com.xdjk.networkdemo.net;

import android.app.Activity;
import android.app.Dialog;

import com.xdjk.networkdemo.listener.LoadingDialogListener;
import com.xdjk.networkdemo.utils.DialogUtils;

import rx.Subscriber;

/**
 * Created by zhanghs on 2017/8/17.
 */

public abstract class NetLoadingDialog<T> extends Subscriber<T> implements LoadingDialogListener{
    private Dialog mDialog;
    private Activity mActivity;

    public NetLoadingDialog(Activity mActivity) {
        this.mActivity = mActivity;
        this.mDialog = DialogUtils.createLoadingDialog(mActivity, "数据加载中..");
    }

    @Override
    public void onStart() {
        super.onStart();
        showLoading();
    }

    @Override
    public void onCompleted() {
        hideLoading();
    }

    @Override
    public void onError(Throwable e) {
        hideLoading();
        onFailure(e);
    }

    @Override
    public void onNext(T t) {
        onResopnse(t);
    }

    @Override
    public void showLoading() {
        if (!mActivity.isFinishing()) {
            mDialog.show();
        }
    }

    @Override
    public void hideLoading() {
        mDialog.cancel();
    }

    /*请求成功返回*/
    public abstract void onResopnse(T response);
    /*请求失败*/
    public abstract void onFailure(Throwable e);
}
